import defaultSetting from "../common/utils/defaultSetting";
import BasicLayout from "../layouts/BasicLayout/index.vue";
import BlankLayout from "../layouts/BlankLayout/index.vue";
import ExploreAi from "../layouts/ExploreAi/index.vue";
import ShotstackLayout from "../layouts/ShotstackLayout/index.vue";
import store from "../store/index";
import components from "./components";
import utils from "../common/utils/utils";
/**
 * @description: 访问路径添加子应用前缀
 * @param {String}path
 * @return {*}
 */
const getPath = (path) => {
  let prefix = "";
  if (window.__POWERED_BY_QIANKUN__) {
    prefix = `/apps/${defaultSetting.name}`; // prefix内容为主应用的activeRule
  }
  path = prefix + path;
  return path;
};

const getRedirectPath = (to) => {
  console.log(to);
  const urlParams = utils.getUrlQuery(null, location.href);
  let source = "";
  if (urlParams.source) {
    if (urlParams.source.indexOf("#/") != -1) {
      let sourceArr = urlParams.source.split("#/");
      if (sourceArr.length > 1) {
        source = sourceArr[0];
      }
    }
  }
  if (source) {
    localStorage.setItem("USER_ACCESS_SOURCE", source);
    if (!localStorage.getItem("USER_ACCESS_SOURCE_INFO")) {
      localStorage.setItem(
        "USER_ACCESS_SOURCE_INFO",
        JSON.stringify({
          user_agent: navigator.userAgent,
          source_url: document.referrer,
        })
      );
    }
    window.location.href =
      location.origin + "/#/sceneModel/work" + location.search;
  } else {
    // 进入第一个子菜单
    const parentRoute = to.matched[0];
    let name = parentRoute.children[0].name;
    return { name };
  }
};

export const customizedRouters = [
    // 基于BasicLayout布局的页面，不需要基础布局的可以不用嵌套路
  
    {
      name: "WorkAIIndex",
      path: "/workShotstack",
      component: components.WorkAIIndex,
      meta: { title: "首页", permission: ["admin"], hidden: true },
    },
    {
      name: "workGPT",
      path: getPath(""),
      component: BasicLayout,
      // redirect: (to) => getRedirectPath(to),
      redirect: "/workShotstack",
      children: [
        {
          name: "SceneModel",
          path: "sceneModel",
          meta: { title: "AI应用", permission: ["admin"], icon: "icon-moxing" },
          children: [
            {
              name: "WorkAI",
              path: "WorkAI",
              component: components.WorkAI,
              meta: {
                title: "官网首页",
                permission: ["admin"],
                icon: "icon-gongzuotai1",
                firstRouteAlias: "SceneModel",
              },
            },
            {
              name: "Collect",
              path: "collect",
              component: components.Collect,
              meta: {
                title: "我的收藏",
                permission: ["admin"],
                icon: "icon-a-bianzu2",
                firstRouteAlias: "SceneModel",
              },
            },
            {
              name: "ApplicationHome",
              path: "index",
              component: components.ApplicationHome,
              meta: {
                title: "全部应用",
                permission: ["admin"],
                firstRouteAlias: "SceneModel",
                icon: "icon-yingyong",
                hidden: true,
              },
            },
            {
              name: "Scene",
              path: "scene/:cateId",
              component: components.Scene,
              meta: { title: "场景", permission: ["admin"], hidden: true },
            },
          ],
        },
  
        // {
        //   name: "Home",
        //   path: "/home",
        //   component: components.Home,
        //   redirect: (to) => {
        //     return {
        //       name: "AIdigitalHuman",
        //     };
        //   },
        //   meta: {
        //     title: "首页",
        //     permission: ["admin"],
        //     icon: "icon-shouye",
        //     firstRouteAlias: "Home",
        //     hidden: true,
        //   },
        // },
   
        {
          name: "Order",
          path: "order",
          component: components.Order,
          meta: { title: "订单管理", hidden: true },
        },
        {
          name: "Vip",
          path: "vip",
          component: components.Vip,
          meta: { title: "会员中心", hidden: true },
        },
        {
          name: "ActivateMembership",
          path: "activateMembership",
          component: components.ActivateMembership,
          meta: { title: "会员开通", hidden: true },
        },
        {
          name: "TeamSetting",
          path: "teamSetting",
          component: components.TeamSetting,
          meta: { title: "团队管理", hidden: true },
        },
        {
          name: "UserSetting",
          path: "userSetting",
          component: components.UserSetting,
          meta: { title: "账号设置", hidden: true },
        },
  
        {
          name: "CreateCenter",
          path: "createCenter",
          component: components.CreateCenter,
          meta: { title: "创作中心", hidden: true },
        },
      ],
    },

    {
      name: "templateScriptSquare",
      path: "/templateScriptSquare",
      component: components.templateScriptSquare,
      meta: {
        title: "",
        hidden: true,
      },
    },
    {
      name: "templateScriptCopy",
      path: "/templateScriptCopy",
      component: components.templateScriptCopy,
      meta: {
        title: "",
        hidden: true,
      },
    },
    {
      name: "LoginPage",
      path: "/loginPage",
      component: components.loginPage,
      meta: { title: "登录", hidden: true },
    },
    {
      name: "workShotstack",
      path: getPath("/workShotstack"),
      redirect: (to) => getRedirectPath(to),
      children: [
        {
          name: "NoticeList",
          path: "notice",
          component: components.NoticeList,
          meta: {
            title: "公告列表",
            permission: ["admin"],
            hidden: true,
          },
        },
        {
          name: "AiCreate",
          path: "aiCreate",
          component: components.AiCreate,
          meta: {
            title: "AI创作工具",
            permission: ["admin"],
            hidden: true,
          },
        },
        {
          name: "Info",
          path: "info",
          component: components.Info,
          meta: {
            title: "主体信息",
            permission: ["admin"],
            hidden: true,
          },
        },
        {
          name: "Download",
          path: "download",
          component: components.Download,
          meta: {
            title: "下载中心",
            permission: ["admin"],
            hidden: true,
          },
        },
        {
          name: "Userinfo",
          path: "userinfo",
          component: components.Userinfo,
          meta: {
            title: "个人信息",
            permission: ["admin"],
            hidden: true,
          },
        },
        {
          name: "ChangePassword",
          path: "changePassword",
          component: components.ChangePassword,
          meta: {
            title: "修改密码",
            permission: ["admin"],
            hidden: true,
          },
        },
        {
          name: "LearningCenter",
          path: "learningCenter",
          component: components.LearningCenter,
          meta: {
            title: "学习中心",
            permission: ["admin"],
            hidden: true,
          },
        },
        {
          name: "digitalHuman",
          path: "digitalHuman",
          meta: {
            title: "数字人分身",
            permission: ["admin"],
            icon: "icon-sucaizhongxin",
          },
          children: [
            {
              name: "AIdigitalHuman",
              path: "AIdigitalHuman",
              component: components.AIdigitalHuman,
              meta: {
                title: "数字人形象",
                permission: ["admin"],
                icon: "icon-shuziren1",
              },
            },
            {
              name: "SoundCloning",
              path: "SoundCloning",
              component: components.SoundCloning,
              meta: {
                title: "声音克隆",
                permission: ["admin"],
                icon: "icon-huatong",
              },
            },
            {
              name: "AIdigitalHumanCreatVideo",
              path: "AIdigitalHumanCreatVideo",
              component: components.AIdigitalHumanCreatVideo,
              meta: {
                title: "视频生成",
                permission: ["admin"],
                icon: "icon-tongyongsucai",
              },
            },
  
            // {
            //   name: "ShopList",
            //   path: "shopList",
            //   component: components.ShopList,
            //   meta: {
            //     title: "项目库",
            //     permission: ["admin"],
            //     icon: "icon-ziyuan",
            //     hidden: true,
            //   },
            // },
          ],
        },
  

        {
          name: "MaterialCenter",
          path: "materialCenter",
          meta: {
            title: "创意素材",
            permission: ["admin"],
            icon: "icon-sucaizhongxin",
          },
          children: [
            {
              name: "ShopList",
              path: "shopList",
              component: components.ShopList,
              meta: {
                title: "项目库",
                permission: ["admin"],
                icon: "icon-ziyuan",
                hidden: true,
              },
            },
            {
              name: "PoiList",
              path: "PoiList",
              component: components.PoiList,
              meta: {
                title: "Poi列表",
                permission: ["admin"],
                icon: "icon-ziyuan",
                hidden: true,
              },
            },
            {
              name: "MaterialList",
              path: "materialList",
              component: components.MaterialList,
              meta: {
                title: "素材库",
                permission: ["admin"],
                icon: "icon-sucaizhongxin",
              },
            },
  
            {
              name: "RecycleBin",
              path: "recycleBin",
              component: components.RecycleBin,
              meta: {
                title: "回收站",
                permission: ["admin"],
                name: "ComposeMaterial",
                icon: "icon-delet",
              },
            },
          ],
        },
  
        {
          name: "MatrixMarketing",
          path: "matrixMarketing",
          meta: {
            title: "发布管理",
            permission: ["admin"],
            icon: "icon-juzhenyingxiao1",
          },
          children: [
            {
              name: "ComposeMaterial",
              path: "composeMaterial",
              component: components.ComposeMaterial,
              meta: {
                title: "剪辑成片",
                permission: ["admin"],
                name: "ComposeMaterial",
                icon: "icon-cz_jl",
              },
            },
            {
              name: "ReleaseList",
              path: "ReleaseList",
              component: components.ReleaseList,
              meta: {
                title: "智能发布",
                permission: ["admin"],
                icon: "icon-fb_jl",
                // icon: "icon-fb_sp",
              },
            },
            {
              name: "IpManage",
              path: "ipManage",
              component: components.IpManage,
              meta: {
                title: "矩阵管理",
                permission: ["admin"],
                icon: "icon-fenfa",
              },
            },
  
            {
              name: "AccountGroupList",
              path: "accountGroupList",
              component: components.AccountGroupList,
              meta: { title: "分组管理", permission: ["admin"], hidden: true },
            },
  
            {
              name: "QrRelease",
              path: "qrRelease",
              component: components.QrRelease,
              meta: { title: "发布成片", permission: ["admin"], hidden: true },
            },
            {
              name: "AutoRelease",
              path: "AutoRelease",
              component: components.QrRelease,
              meta: { title: "成片发布", permission: ["admin"], hidden: true },
            },
            {
              name: "ReleaseDetail",
              path: "releaseDetail",
              component: components.ReleaseDetail,
              meta: {
                title: "发布详情",
                permission: ["admin"],
                hidden: true,
              },
            },
            {
              name: "BurstShop",
              path: "BurstShop",
              component: components.BurstShop,
              meta: {
                title: "爆店码",
                permission: ["admin"],
                icon: "icon-iconfontscan",
                // hidden: true,
              },
            },
            {
              name: "BurstShopDetail",
              path: "BurstShopDetail",
              component: components.BurstShopDetail,
              meta: {
                title: "添加爆店码",
                permission: ["admin"],
                icon: "icon-douyinkapian",
                hidden: true,
              },
            },
            {
              name: "BurstShopDetails",
              path: "BurstShopDetails",
              component: components.BurstShopDetails,
              meta: {
                title: "爆店码明细",
                permission: ["admin"],
                icon: "icon-douyinkapian",
                hidden: true,
              },
            },
          ],
        },
        // 微信管理--新增
         {
          name: "WechatMarketing",
          path: "wechatMarketing",
          meta: {
            title: "微信管理",
            icon: "icon-juzhenyingxiao1",
          },
          children: [
            {
              name: "WechatComposeMaterial",
              path: "wechatcomposeMaterial",
              component: components.ComposeMaterial,
              meta: {
                title: "矩阵管理",
                name: "WechatComposeMaterial",
                icon: "icon-cz_jl",
              },
            }
          ],
        },
  
        {
          name: "PersonalCenter",
          path: "personalCenter",
          meta: {
            title: "个人中心",
            permission: ["admin"],
            icon: "icon-gerenzhongxin1",
          },
          children: [
            {
              name: "Index",
              path: "index",
              component: components.Index,
              meta: {
                title: "员工管理",
                permission: ["admin"],
                icon: "icon-yuangong",
              },
            },
            {
              name: "BusinessRecord",
              path: "businessRecord",
              component: components.BusinessRecord,
              meta: {
                title: "账户管理",
                permission: ["admin"],
                icon: "icon-zhanghu",
              },
            },
            {
              name: "PromotionLink",
              path: "promotionLink",
              component: components.PromotionLink,
              meta: {
                title: "邀请用户",
                permission: ["admin"],
                icon: "icon-yaoqing",
              },
            },
            {
              name: "VoucherCenter",
              path: "voucherCenter",
              component: components.VoucherCenter,
              meta: { title: "充值中心", permission: ["admin"], hidden: true },
            },
            {
              name: "InviteNewUsers",
              path: "inviteNewUsers",
              component: components.InviteNewUsers,
              meta: { title: "推广链接", permission: ["admin"], hidden: true },
            },
          ],
        },
        {
          name: "MemberBenefits",
          path: "memberBenefits",
          component: components.MemberBenefits,
          meta: { title: "会员权益", hidden: true },
        },
        {
          name: "TagManage",
          path: "tagManage",
          component: components.TagManage,
          meta: { title: "标签管理", hidden: true },
        },
        {
          name: "ItemTag",
          path: "itemTag",
          component: components.ItemTag,
          meta: { title: "项目标签列表", hidden: true },
        },
      ],
    },
    {
      name: "custom",
      path: getPath("/custom"),
      component: BlankLayout,
      redirect: (to) => getRedirectPath(to),
      children: [
        {
          name: "TextPreview",
          path: "textPreview",
          component: components.TextPreview,
          meta: { title: "" },
        },
      ],
    },
    {
      name: "UseAgreement",
      path: "/UseAgreement",
      component: components.UseAgreement,
      meta: { title: "用户注册协议", hidden: true },
    },
    {
      name: "PrivacyAgreement",
      path: "/PrivacyAgreement",
      component: components.PrivacyAgreement,
      meta: { title: "用户隐私协议", hidden: true },
    },
];

// 获取所有需要的路由的 name列表
export const haveRouters = () => {
  const extract = routes => 
    routes.flatMap(({ name, children }) => [
      ...(name ? [name] : []),  // 过滤无效 name
      ...extract(children || []) 
    ])
  const arr = extract(customizedRouters)
  return arr
}